package com.carlinfo.back.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.DateUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.pojo.ADynasty;
import com.carlinfo.service.IDemoService;
import com.carlinfo.service.impl.DemoServiceImpl;

/**
 *  一个模块对应一个Service
 * 朝代,还有皇上:CRUD
 * @author WangshMac
 */
public class DemoBackServlet extends HttpServlet
{
	private Logger logger = LogManager.getLogger() ; 
	private DateUtil dateUtil = new DateUtil() ; 
	
	/*	 创建一个demoService	 */
	private IDemoService demoService = new DemoServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/*	乱码处理的简化版	*/
		request.setCharacterEncoding("UTF-8");
		this.logger.info("--DemoBackServlet--service--");
		
		String method = request.getParameter("method");
		/*	处理乱码最万能的是	 */
		if("get".equalsIgnoreCase(request.getMethod()))
		{
			/*	 解码:把字符串变成字节数组 	*/
			byte[] b = method.getBytes("ISO8859-1");
			/* 	
			 * 	编码不一样
			 */
			method = new String(b,"UTF-8");
		}
		this.logger.info("--DemoBackServlet--method:{}--",method);
		if("dynastyList".equalsIgnoreCase(method))
		{
			/*	 查询多条朝代	*/
			this.dynastyList(request,response);
		}else if("dynastyInsert".equalsIgnoreCase(method))
		{
			/* 	打开朝代添加的页面	*/
			this.dynastyInsert(request,response);
		}else if("dynastyInsertSubmit".equalsIgnoreCase(method))
		{
			/* 	朝代添加提交的页面	 */
			this.dynastyInsertSubmit(request,response);
		}
	}
	
	/**
	 *	 朝代添加提交
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyInsertSubmit--");
		/* 从jsp中获取 */
		String name = request.getParameter("name");
		String capital = request.getParameter("capital");
		String age = request.getParameter("age");
		String st = request.getParameter("st");
		String pubTime = request.getParameter("pubTime");
		String context = request.getParameter("context");
		String status = request.getParameter("status");
		
		/* new出来的,属性有值? */
		ADynasty dynasty = new ADynasty() ; 
		dynasty.setName(name);
		dynasty.setCapital(capital);
		/* 420--->266: */
		dynasty.setAge(Integer.valueOf(age));
		dynasty.setContext(context);
		/* dateUtil */
		dynasty.setSt(this.dateUtil.strToDateTime(st));
		dynasty.setPubTime(this.dateUtil.strToDateTime(pubTime));
		
		dynasty.setStatus(Byte.valueOf(status));
		dynasty.setCreateTime(new Date());
		dynasty.setUpdateTime(new Date());
		
		/* 保存一条记录 */
		JSONObject resultJSON = this.demoService.saveOneDynastyService(dynasty);
		this.logger.info("返回值:{}",resultJSON);
		
		/*	 往request中放的是json 	*/
		request.setAttribute("resultJSON", resultJSON);
		/*	保存成功,又跳转到朝代添加的jsp中	*/
		this.dynastyInsert(request, response);
	}

	/**
	 * 	打开朝代添加的页面
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyInsert--");
		/* 	服务器端跳转:	*/
		String path = "/WEB-INF/jsp/back/demo/dynastyInsert.jsp" ; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 	查询多条朝代
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void dynastyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("--DemoBackServlet--dynastyList--");
		/*	 --实现搜索	 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		/* 处理参数 */
		if(keyword == null)
		{
			keyword = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		/*	 为了实现分页;	*/
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		/*	 查询10条	 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		try
		{
			
			/*	 每页10条	, */
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
			/* 	第一页 	*/
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
		} catch (NumberFormatException e)
		{
		}
		
		/*	 存储的是查询条件	 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*	 按照关键字搜索	 */
		condMap.put("keyword", keyword);
		/*	 按照状态搜索	 */
		condMap.put("status", status);
		/*	 按照时间范围搜索
		 * 	stDate:开始时间
		 * 	edDate:结束时间
		 */
		Date edDate = null ; 
		Date stDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.strToDateTime(st) ;
			edDate = this.dateUtil.strToDateTime(ed) ;
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		//this.logger.info("{}----->{}",stDate.toLocaleString(),edDate.toLocaleString());
		
		List<ADynasty> dynastyList = this.demoService.selectListDynastyService(pageInfoUtil,condMap);
		/*	 站在使用者的角度,不想分页,直接将pageInfoUtil传成null 	*/
		request.setAttribute("dynastyList", dynastyList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/*	 将所有的搜索条件存储 	*/
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		/* 	服务器端跳转:	*/
		String path = "/WEB-INF/jsp/back/demo/dynastyList.jsp" ; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}
